<template>
  <div>
    <h1>当前求和为：{{sum}}</h1>
    <h3>当前求和的10倍：{{bigSum}}</h3>
    <h4>学校名:{{school}},学校地址:{{address}}</h4>
    <h4 style="color:red;">person组件中的人数和为：{{personList.length}}</h4>
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementOdd">和为奇数时加</button>
    <button @click="incrementWait">过一段时间再加</button>
  </div>
</template>

<script>
import {mapState} from "vuex"
import {mapGetters} from 'vuex'
export default {
  name:"Count",
  data(){
    return {
      n:1,
    }
  },
  computed:{
    //mapState可以直接获得$store.state中的数据(对象写法)
    // ...mapState({sum:"sum",school:"school",address:"address"}),
    //mapState可以直接获得$store.state中的数据(数组写法)
    ...mapState(['sum','school','address','personList']),
    
    //mapGetters可以直接获得$store.getters中的数据
    ...mapGetters({bigSum:"bigSum"})
  },
  methods:{
    increment(){
      this.$store.commit('JIA',this.n);
    },
    decrement(){
      this.$store.commit('JIAN',this.n)
    },
    incrementOdd(){
      this.$store.dispatch('jiaOdd',this.n);
    },
    incrementWait(){
      this.$store.dispatch('jiaWait',this.n);
    }
  },
  mounted(){
    console.log(this);
  }
}
</script>

<style>
  button{
    margin-left: 5px;
  }
</style>